using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventManager : MonoBehaviour
{
    //事件的回调
    public delegate void EventHandler(Object args);

    //事件集合
    Dictionary<int, EventHandler> events = new Dictionary<int, EventHandler>();

    public void Subscribe(int _id, EventHandler _event)
    {
        if(events.ContainsKey(_id))
            events[_id] += _event;
        else
            events.Add(_id, _event);
    }

    public void Unsubscribe(int _id, EventHandler _event) 
    {
        if( events.ContainsKey(_id))
        {
            if (events[_id] != null)
                events[_id] -= _event;

            if (events[_id] == null)
                events.Remove(_id);
        }
    }

    public void Fire(int _id, Object args = null)
    {
        EventHandler _event;
        if(events.TryGetValue(_id, out _event))
        {
            _event(args);
        }
    }
}
